var data = require("./data.js");
var clickjs = require("./click.js");

var define = data.define;

var last_x = -1;
var last_y = -1;
var times = 0;
function createLight(map, mapW, mapH, width, mapMouse) {
  if (!clickjs.canInput(map, mapW, mapH, width, mapMouse))
    return;

  var x = parseInt(clickjs.getX());
  var y = parseInt(clickjs.getY());

  if (last_x != -1 && map[last_x][last_y] == define.Light)
    map[last_x][last_y] = 0;

  if ((!data.isWall(map[x][y]) && map[x][y] != define.Block))
    map[x][y] = define.Light;

  last_x = x;
  last_y = y;
  times = 0;
}

function clear() {
  last_x = -1;
  last_y = -1;
  times=0;
}

function updateLight(map, mapW, mapH, width, mapMouse) {
  times++;
  if (times >= (define.LightTime / define.ShowDelay)) 
    clear();
}

function getInfo() {
  var temp = {};
  temp.x = last_x;
  temp.y = last_y;
  return temp;
}

module.exports = {
  createLight: createLight,
  updateLight: updateLight,
  clear: clear,
  getInfo: getInfo
}